﻿using GalaSoft.MvvmLight.Messaging;
namespace MetaVideoEditorEvolved
{

	using System.Windows;

	/// <summary>
	/// Interaction logic for SplahScreen.xaml
	/// </summary>
	public partial class SplahScreen : Window
	{
		private bool isRebuild;
		
		private ViewModel.SplashScreenVM vm;

		public SplahScreen()
		{
			InitializeComponent();
			
			vm = new ViewModel.SplashScreenVM(this.isRebuild);
			this.DataContext = vm;
		}

		public SplahScreen(bool isRebuild)
			: this()
		{
			this.isRebuild = isRebuild;

			if (System.Windows.Application.Current.MainWindow != null && System.Windows.Application.Current.MainWindow != this)
			{
				this.Owner = System.Windows.Application.Current.MainWindow;
			}
			
			vm = new ViewModel.SplashScreenVM(this.isRebuild);
			this.DataContext = vm;
			
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			Messenger.Default.Register<Messages.CloseMessage>(this, (message) =>
			                                                  {
			                                                  	if (message.WindowToClose == Messages.WindowType.SplashScreen)
			                                                  	{
			                                                  		this.Close();
			                                                  	}
			                                                  });

			Messenger.Default.Register<Messages.OpenWindowMessage>(this, (message) =>
			                                                       {
			                                                       	if (message.WindowToOpen == Messages.WindowType.MainWindow)
			                                                       	{
			                                                       		var mainWindow = new MainWindow();
			                                                       		mainWindow.Show();
			                                                       	}
			                                                       });
								
			vm.LoadedCommand.Execute(null);
		}
	}
}
